import {RequestHandler as Middleware, Request, Response, NextFunction} from 'express';

export const requestLogger: Middleware = (req: Request, res: Response, next: NextFunction) => {
    let msg = `请求时间：${new Date().toLocaleTimeString()} ---------- 请求路径：${req.path}  `;
    // log 请求信息
    if (req.method === 'GET') {
        msg += `请求方式：${req.method} \n ---- 查询参数：${JSON.stringify(req.query)} \n ---- 路径参数：${JSON.stringify(req.params)}`;
    } else if (req.method === 'POST') {
        msg += `请求方式：${req.method} \n ---- 请求体参数为：${JSON.stringify(req.body)} \n ---- 查询参数：${JSON.stringify(req.query)} \n ---- 路径参数：${JSON.stringify(req.params)}`;
    } else if (req.method === 'DELETE') {
        msg += `请求方式：${req.method} \n ---- 请求体参数为：${JSON.stringify(req.body)} \n ---- 查询参数：${JSON.stringify(req.query)} \n ---- 路径参数：${JSON.stringify(req.params)}`;
    }
    console.log(msg);
    next();
};
